/*
 * @Date: 2021-08-17 17:07:59
 * @information:
 */
import Util from '../utils/util'
import LikeDiaryDao from '../dao/LikeDiaryDao'

const likeDiaryDao = new LikeDiaryDao()


export default class LikeDiaryService {
  async findByDiaryId (data) {
    const { diaryId } = data
    const obj = { diaryId }
    Util.delNotKey(obj)
    Util.checkObj(obj, ['diaryId'])
    const res = await likeDiaryDao.findByDiaryId({ diaryId })
    return res
  }

  async likeDiary (data) {
    const { diaryId, toUserId, fromUserId } = data
    const obj = { diaryId, toUserId, fromUserId }
    Util.delNotKey(obj)
    Util.checkObj(obj, ['diaryId', 'toUserId', 'fromUserId'])
    const res = await likeDiaryDao.findByDiaryIdAndFromUserIdAndToUserId(obj)
    if (res) {
      const result = await likeDiaryDao.recover({ id: res.id })
      return res
    } else {
      const result = await likeDiaryDao.add(obj)
      return result
    }
  }

  async unLikeDiary (data) {
    const { diaryId, toUserId, fromUserId } = data
    const obj = { diaryId, toUserId, fromUserId }
    Util.delNotKey(obj)
    Util.checkObj(obj, ['diaryId', 'toUserId', 'fromUserId'])
    const res = await likeDiaryDao.findByDiaryIdAndFromUserIdAndToUserId(obj)
    if (res) {
      const result = await likeDiaryDao.del({ id: res.id })
      return null
    }
  }



}